﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data.SqlClient;
using System.Configuration;
using Dapper;
using System.Data;

namespace DAL
{
    public class EquInfoDAL
    {
        public static readonly string SqlConnString = ConfigurationManager.ConnectionStrings["ELCO_ConnectionString"].ConnectionString;


        public static List<EquKaoHaoInfo> initCustomerInfo()
        {
            using (var conn = new SqlConnection(SqlConnString))
            {

                string str = "select * from EquTypeInfo order by EquIndex";
                return conn.Query<EquKaoHaoInfo>(str).ToList();
            }

        }

        /// <summary>
        /// 根据设备类型获取 设备台号
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static List<EquInfo> GetEquNoList(string EquType, string CustomerID)
        {

            using (var conn = new SqlConnection(SqlConnString))
            {

                string str = "select ID,EquNo,EquType,Remark from EquipmentInfo where EquType='" + EquType + "' and EquNo not in (select EquNo from CustomerEquInfo where CustomerID='"+CustomerID+"')";
                List<EquInfo> list = conn.Query<EquInfo>(str).ToList();
                for (int i = 0; i < list.Count(); i++ )
                {
                    list[i].IsActive = "0";
                }
                return list;
            }
        }

        /// <summary>
        /// 保存设备类型信息
        /// </summary>
        /// <param name="EquTypeName"></param>
        /// <param name="EquTypeRemark"></param>
        /// <param name="EquIndex"></param>
        /// <returns></returns>
        public static string addEquTypeInfo(string EquTypeName, string EquTypeRemark, string EquIndex)
        {
            using (var conn = new SqlConnection(SqlConnString))
            {
                SqlCommand cmd = new SqlCommand();
                SqlTransaction transaction = null;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    cmd.Transaction = transaction;
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlParameter[] sqlPara = new SqlParameter[4];

                    sqlPara[0] = new SqlParameter("@EquTypeName", EquTypeName);
                    sqlPara[0].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[1] = new SqlParameter("@EquTypeRemark", EquTypeRemark);
                    sqlPara[1].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[2] = new SqlParameter("@EquIndex", EquIndex);
                    sqlPara[2].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[3] = new SqlParameter("@CatchFlag", 0);
                    sqlPara[3].Size = 10;
                    sqlPara[3].Direction = System.Data.ParameterDirection.Output;

                    foreach (SqlParameter para in sqlPara)
                    {
                        cmd.Parameters.Add(para);
                    }
                    cmd.CommandText = "[usp_Equ_AddEquTypeInfo]";
                    cmd.ExecuteNonQuery();
                    transaction.Commit();
                    if (int.Parse(sqlPara[3].Value.ToString()) == 1)
                    {
                        return "hasexit";
                    }
                    return "success";
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return "falut";
                }
            }

        }


        /// <summary>
        /// 保存新增设备卡号
        /// </summary>
        /// <param name="EquNo"></param>
        /// <param name="EquType"></param>
        /// <param name="Remark"></param>
        /// <returns></returns>
        public static string addEquKaoHao(string EquNo, string EquType, string Remark)
        {
            using (var conn = new SqlConnection(SqlConnString))
            {
                SqlCommand cmd = new SqlCommand();
                SqlTransaction transaction = null;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    cmd.Transaction = transaction;
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlParameter[] sqlPara = new SqlParameter[4];

                    sqlPara[0] = new SqlParameter("@EquNo", EquNo);
                    sqlPara[0].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[1] = new SqlParameter("@EquType", EquType);
                    sqlPara[1].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[2] = new SqlParameter("@Remark", Remark);
                    sqlPara[2].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[3] = new SqlParameter("@CatchFlag", 0);
                    sqlPara[3].Size = 10;
                    sqlPara[3].Direction = System.Data.ParameterDirection.Output;

                    foreach (SqlParameter para in sqlPara)
                    {
                        cmd.Parameters.Add(para);
                    }
                    cmd.CommandText = "[usp_Equ_AddEquKaoHao]";
                    cmd.ExecuteNonQuery();
                    transaction.Commit();
                    if (int.Parse(sqlPara[3].Value.ToString()) == 1)
                    {
                        return "hasexit";
                    }
                    return "success";
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return "falut";
                }
            }

        }


        /// <summary>
        /// 设置纺纱品种
        /// </summary>
        /// <param name="EquNo"></param>
        /// <param name="FangShaKind"></param>
        /// <returns></returns>
        public static string savefangshakind(string TaiHao,string EquNo, string FangShaKind,string FangShaDate, string CustomerID)
        {
            using (var conn = new SqlConnection(SqlConnString))
            {
                SqlCommand cmd = new SqlCommand();
                SqlTransaction transaction = null;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    cmd.Transaction = transaction;
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlParameter[] sqlPara = new SqlParameter[5];

                    sqlPara[0] = new SqlParameter("@TaiHao", TaiHao);
                    sqlPara[0].Direction = System.Data.ParameterDirection.Input;

                    sqlPara[1] = new SqlParameter("@EquNo", EquNo);
                    sqlPara[1].Direction = System.Data.ParameterDirection.Input;

                    sqlPara[2] = new SqlParameter("@FangShaKind", FangShaKind);
                    sqlPara[2].Direction = System.Data.ParameterDirection.Input;
                    
                    sqlPara[3] = new SqlParameter("@FangShaDate", FangShaDate);
                    sqlPara[3].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[4] = new SqlParameter("@CustomerID", CustomerID);
                    sqlPara[4].Direction = System.Data.ParameterDirection.Input;

                    foreach (SqlParameter para in sqlPara)
                    {
                        cmd.Parameters.Add(para);
                    }
                    cmd.CommandText = "[usp_Equ_Setfangshakind]";
                    cmd.ExecuteNonQuery();
                    transaction.Commit();
                    return "success";
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return "falut";
                }
            }

        }

        /// <summary>
        ///  设置 维护周期
        /// </summary>
        /// <param name="TaiHao"></param>
        /// <param name="MaintenanceCycle"></param>
        /// <param name="CustomerID"></param>
        /// <returns></returns>
        public static string saveMaintenanceCycle(string TaiHao,string EquNo,string MaintenanceCycle,string CustomerID)
        {
            using (var conn = new SqlConnection(SqlConnString))
            {
                SqlCommand cmd = new SqlCommand();
                SqlTransaction transaction = null;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    cmd.Transaction = transaction;
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlParameter[] sqlPara = new SqlParameter[4];

                    sqlPara[0] = new SqlParameter("@TaiHao", TaiHao);
                    sqlPara[0].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[1] = new SqlParameter("@MaintenanceCycle", MaintenanceCycle);
                    sqlPara[1].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[2] = new SqlParameter("@CustomerID", CustomerID);
                    sqlPara[2].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[3] = new SqlParameter("@EquNo", EquNo);
                    sqlPara[3].Direction = System.Data.ParameterDirection.Input;

                    foreach (SqlParameter para in sqlPara)
                    {
                        cmd.Parameters.Add(para);
                    }
                    cmd.CommandText = "[usp_Equ_SetMaintenanceCycle]";
                    cmd.ExecuteNonQuery();
                    transaction.Commit();
                    return "success";
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return "falut";
                }
            }

        }

        /// <summary>
        /// 设备维护操作
        /// </summary>
        /// <param name="TaiHao"></param>
        /// <param name="CustomerID"></param>
        /// <returns></returns>
        public static string maintenanceEquInfo(string TaiHao,string EquNo, string CustomerID)
        {
            using (var conn = new SqlConnection(SqlConnString))
            {
                SqlCommand cmd = new SqlCommand();
                SqlTransaction transaction = null;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    cmd.Transaction = transaction;
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlParameter[] sqlPara = new SqlParameter[3];

                    sqlPara[0] = new SqlParameter("@TaiHao", TaiHao);
                    sqlPara[0].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[1] = new SqlParameter("@EquNo", EquNo);
                    sqlPara[1].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[2] = new SqlParameter("@CustomerID", CustomerID);
                    sqlPara[2].Direction = System.Data.ParameterDirection.Input;

                    foreach (SqlParameter para in sqlPara)
                    {
                        cmd.Parameters.Add(para);
                    }
                    cmd.CommandText = "[usp_Equ_maintenanceEquInfo]";
                    cmd.ExecuteNonQuery();
                    transaction.Commit();
                    return "success";
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return "falut";
                }
            }

        }

        /// <summary>
        /// 初始化设备类型列表
        /// </summary>
        /// <param name="sortname"></param>
        /// <param name="sortorder"></param>
        /// <param name="keyvalue"></param>
        /// <param name="EquIndex"></param>
        /// <returns></returns>
        public static DataTable GetEquTypeInfo(string sortname, string sortorder, string keyvalue, string EquIndex)
        {
            string str = "";
            DataTable dt = new DataTable();
            using (var conn = new SqlConnection(SqlConnString))
            {
                SqlCommand cmd = new SqlCommand();
                conn.Open();
                cmd.Connection = conn;

                str = "select type.ID,feature.ID as FeatureID,type.EquTypeName,type.EquTypeRemark,feature.EquTypeFeatures,feature.FeaturesContent,feature.MaintenanceCycle,feature.ChangeEquNo  from EquTypeInfo type left join EquTypeFeatures feature on type.EquTypeName = feature.EquTypeName where type.EquIndex='" + EquIndex + "'";
                if (keyvalue != "")
                {
                    str += " and type.EquTypeName like '%" + keyvalue + "%'";
                }
                str += "order by type." + sortname + " " + sortorder + "";
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = str;
                SqlDataAdapter Datapter = new SqlDataAdapter(cmd);
                Datapter.Fill(dt);
                return dt;

            }

        }
        
        /// <summary>
        /// 初始化设备故障信息 列表
        /// </summary>
        /// <param name="sortname"></param>
        /// <param name="sortorder"></param>
        /// <param name="keyvalue"></param>
        /// <returns></returns>
        public static DataTable GetEquAlarmInfo(string sortname, string sortorder, string keyvalue)
        {
            string str = "";
            DataTable dt = new DataTable();
            using (var conn = new SqlConnection(SqlConnString))
            {
                SqlCommand cmd = new SqlCommand();
                conn.Open();
                cmd.Connection = conn;

                str = "select * from RegisterAlarmInfo";
                if (keyvalue != "")
                {
                    str += " and AlarmName like '%" + keyvalue + "%'";
                }
                str += " order by " + sortname + " " + sortorder + "";
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = str;
                SqlDataAdapter Datapter = new SqlDataAdapter(cmd);
                Datapter.Fill(dt);
                return dt;

            }

        }

        /// <summary>
        /// 当是  宏大用户登录  获取设备列表信息
        /// </summary>
        /// <param name="sortname"></param>
        /// <param name="sortorder"></param>
        /// <param name="keyvalue"></param>
        /// <returns></returns>
        public static DataTable initEquListByHongda(string sortname, string sortorder, string keyvalue, string CustomerName)
        {
            string str = "";
            DataTable dt = new DataTable();
            using (var conn = new SqlConnection(SqlConnString))
            {
                SqlCommand cmd = new SqlCommand();
                conn.Open();
                cmd.Connection = conn;
                if (CustomerName == "宏大")
                {
                    str = "select a.ID, a.EquNo,a.EquType,a.CreateTime,customer.CustomName,b.TaiHao,a.Remark,customer.UseDate from EquipmentInfo a left join CustomerEquInfo b on  a.EquNo=b.EquNo left join CustomInfo customer on b.CustomerID=customer.ID where 1=1 ";
                    if (keyvalue != "")
                    {
                        str += " and a.EquNo like '%" + keyvalue + "%'";
                    }
                    str += "order by a.EquType desc,b.TaiHao";
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = str;
                    SqlDataAdapter Datapter = new SqlDataAdapter(cmd);
                    Datapter.Fill(dt);
                    return dt;
                }
                else
                {
                    str = "select a.ID, a.EquNo,a.EquType,a.CreateTime,b.FangShaKind,a.Remark  from EquipmentInfo a left join CustomerEquInfo b on  a.EquNo=b.EquNo where b.CustomerID=(select ID from CustomInfo where CustomName='" + CustomerName + "') ";
                    if (keyvalue != "")
                    {
                        str += " and a.EquNo like '%" + keyvalue + "%'";
                    }
                    str += "order by a." + sortname + " " + sortorder + "";
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = str;
                    SqlDataAdapter Datapter = new SqlDataAdapter(cmd);
                    Datapter.Fill(dt);
                    return dt;
                }
            }

        }

        /// <summary>
        /// 当是 客户登录 获取所购设备列表
        /// </summary>
        /// <param name="sortname"></param>
        /// <param name="sortorder"></param>
        /// <param name="keyvalue"></param>
        /// <param name="CustomerName"></param>
        /// <returns></returns>
        public static DataTable initEquListByCustomerID(string sortname, string sortorder, string keyvalue, string CustomerName)
        {
            string str = "";
            DataTable dataTable = new DataTable();
            using (SqlConnection connection = new SqlConnection(SqlConnString))
            {
                SqlCommand selectCommand = new SqlCommand();
                connection.Open();
                selectCommand.Connection = connection;
                str = "select  customerequ.ID,customerequ.CustomerID,customerequ.EquNo, customerequ.TaiHao,customerequ.EquType, fangsha.FangShaKind,fangsha.UpdateTime,customeinfo.UseDate,customerequ.MaintenanceCycle,customerequ.MaintenanceDate from CustomerEquInfo customerequ left join EquipmentInfo equ on customerequ.EquNo = equ.EquNo left join CustomerTaiHaoFangShaInfo fangsha on customerequ.CustomerID = fangsha.CustomerID and customerequ.TaiHao = fangsha.TaiHao and customerequ.EquNo = fangsha.EquNo left join CustomInfo customeinfo on customerequ.CustomerID = customeinfo.ID where customerequ.CustomerID = (select ID from CustomInfo where CustomName='" + CustomerName + "') order by customerequ.EquType desc,customerequ.TaiHao";
                selectCommand.CommandType = CommandType.Text;
                selectCommand.CommandText = str;
                new SqlDataAdapter(selectCommand).Fill(dataTable);
                return dataTable;
            }
        }
     
        /// <summary>
        /// 删除设备类型
        /// </summary>
        /// <param name="TypeID"></param>
        /// <returns></returns>
        public static int deleteEquTypeInfo(string TypeID, string FeatureID)
        {
            int affecgtRow = 0;

            using (var conn = new SqlConnection(SqlConnString))
            {
                string sql = "DELETE FROM  EquTypeInfo WHERE ID = @TypeID;";
                string sql1 = "DELETE FROM  EquTypeFeatures WHERE ID = @FeatureID;";
                affecgtRow = conn.Execute(sql + sql1, new { TypeID = TypeID, FeatureID = FeatureID});
            }
            return affecgtRow;
        }



        /// <summary>
        /// 删除设备信息
        /// </summary>
        /// <param name="EquID"></param>
        /// <returns></returns>
        public static int deleteEquInfo(string EquID)
        {
            int affecgtRow = 0;

            using (var conn = new SqlConnection(SqlConnString))
            {
                string sql = "DELETE FROM  EquipmentInfo WHERE ID = @EquID;";
                affecgtRow = conn.Execute(sql, new { EquID = EquID });
            }
            return affecgtRow;
        }

        public static int deleteEquTypeInfoguidao(string EquID)
        {
            int affecgtRow = 0;

            using (var conn = new SqlConnection(SqlConnString))
            {
                string sql = "DELETE FROM  EquTypeInfo WHERE ID = @EquID;";
                affecgtRow = conn.Execute(sql, new { EquID = EquID });
            }
            return affecgtRow;
        }

        /// <summary>
        /// 编辑设备类型初始化界面
        /// </summary>
        /// <param name="TypeID"></param>
        /// <returns></returns>
        public static List<EquKaoHaoInfo> initeditequtype(string TypeID, string FeatureID)
        {
            using (var conn = new SqlConnection(SqlConnString))
            {
                string str = string.Empty;

                if(FeatureID == "") {
                    str = "select type.ID,type.EquTypeName,type.EquTypeRemark,type.EquIndex,feature.EquTypeFeatures,feature.FeaturesContent,feature.MaintenanceCycle,feature.ChangeEquNo from EquTypeInfo type  left join EquTypeFeatures feature on type.EquTypeName = feature.EquTypeName where type.ID = '" + TypeID + "' and  feature.ID is null";

                }
                else
                {
                    str = "select type.ID,type.EquTypeName,type.EquTypeRemark,type.EquIndex,feature.EquTypeFeatures,feature.FeaturesContent,feature.MaintenanceCycle,feature.ChangeEquNo from EquTypeInfo type  left join EquTypeFeatures feature on type.EquTypeName = feature.EquTypeName where type.ID = '" + TypeID + "' and  feature.ID ='" + FeatureID + "'";
                }
                return conn.Query<EquKaoHaoInfo>(str).ToList();
            }

        }


        /// <summary>
        /// 初始化设备报警编辑界面
        /// </summary>
        /// <param name="AlarmID"></param>
        /// <returns></returns>
        public static List<ShengchanCushaAlarm> initeditequalarm(string AlarmID)
        {
            using (var conn = new SqlConnection(SqlConnString))
            {
                string str = string.Empty;

                str = "select '',AlarmName,AlarmCode,AlarmHandle,note from RegisterAlarmInfo where id='"+AlarmID+"'";
                
                return conn.Query<ShengchanCushaAlarm>(str).ToList();
            }

        }

        /// <summary>
        /// 保存编辑设备类型
        /// </summary>
        /// <param name="TypeID"></param>
        /// <param name="EquTypeName"></param>
        /// <param name="EquTypeRemark"></param>
        /// <param name="EquIndex"></param>
        /// <returns></returns>
        public static string editequtype(string TypeID,string FeatureID, string EquTypeName, string EquTypeRemark, string EquIndex, string EquTypeFeatures, string FeaturesContent, string MaintenanceCycle, string ChangeEquNo)
        {
            using (var conn = new SqlConnection(SqlConnString))
            {
                SqlCommand cmd = new SqlCommand();
                SqlTransaction transaction = null;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    cmd.Transaction = transaction;
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlParameter[] sqlPara = new SqlParameter[10];

                    sqlPara[0] = new SqlParameter("@TypeID", TypeID);
                    sqlPara[0].Direction = System.Data.ParameterDirection.Input;

                    sqlPara[1] = new SqlParameter("@FeatureID", FeatureID);
                    sqlPara[1].Direction = System.Data.ParameterDirection.Input;

                    sqlPara[2] = new SqlParameter("@EquTypeName", EquTypeName);
                    sqlPara[2].Direction = System.Data.ParameterDirection.Input;

                    sqlPara[3] = new SqlParameter("@EquTypeRemark", EquTypeRemark);
                    sqlPara[3].Direction = System.Data.ParameterDirection.Input;

                    sqlPara[4] = new SqlParameter("@EquIndex", EquIndex);
                    sqlPara[4].Direction = System.Data.ParameterDirection.Input;

                    sqlPara[5] = new SqlParameter("@EquTypeFeatures", EquTypeFeatures);
                    sqlPara[5].Direction = System.Data.ParameterDirection.Input;

                    sqlPara[6] = new SqlParameter("@FeaturesContent", FeaturesContent);
                    sqlPara[6].Direction = System.Data.ParameterDirection.Input;

                    sqlPara[7] = new SqlParameter("@MaintenanceCycle", MaintenanceCycle);
                    sqlPara[7].Direction = System.Data.ParameterDirection.Input;

                    sqlPara[8] = new SqlParameter("@ChangeEquNo", ChangeEquNo);
                    sqlPara[8].Direction = System.Data.ParameterDirection.Input;

                    sqlPara[9] = new SqlParameter("@CatchFlag", 0);
                    sqlPara[9].Size = 10;
                    sqlPara[9].Direction = System.Data.ParameterDirection.Output;

                    foreach (SqlParameter para in sqlPara)
                    {
                        cmd.Parameters.Add(para);
                    }
                    cmd.CommandText = "[usp_Equ_EditEquType]";
                    cmd.ExecuteNonQuery();
                    transaction.Commit();
                    if (int.Parse(sqlPara[9].Value.ToString()) == 1)
                    {
                        return "hasexit";
                    }
                    return "success";
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return "falut";
                }
            }

        }


        public static string editequtypeguidao(string TypeID, string EquTypeName, string EquTypeRemark, string EquIndex)
        {
            using (var conn = new SqlConnection(SqlConnString))
            {
                SqlCommand cmd = new SqlCommand();
                SqlTransaction transaction = null;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    cmd.Transaction = transaction;
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlParameter[] sqlPara = new SqlParameter[5];

                    sqlPara[0] = new SqlParameter("@TypeID", TypeID);
                    sqlPara[0].Direction = System.Data.ParameterDirection.Input;

                    sqlPara[1] = new SqlParameter("@EquTypeName", EquTypeName);
                    sqlPara[1].Direction = System.Data.ParameterDirection.Input;

                    sqlPara[2] = new SqlParameter("@EquTypeRemark", EquTypeRemark);
                    sqlPara[2].Direction = System.Data.ParameterDirection.Input;

                    sqlPara[3] = new SqlParameter("@EquIndex", EquIndex);
                    sqlPara[3].Direction = System.Data.ParameterDirection.Input;

                    sqlPara[4] = new SqlParameter("@CatchFlag", 0);
                    sqlPara[4].Size = 10;
                    sqlPara[4].Direction = System.Data.ParameterDirection.Output;

                    foreach (SqlParameter para in sqlPara)
                    {
                        cmd.Parameters.Add(para);
                    }
                    cmd.CommandText = "[usp_Equ_EditEquTypeGuidao]";
                    cmd.ExecuteNonQuery();
                    transaction.Commit();
                    if (int.Parse(sqlPara[4].Value.ToString()) == 1)
                    {
                        return "hasexit";
                    }
                    return "success";
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return "falut";
                }
            }

        }

        /// <summary>
        /// 保存设备类型特性
        /// </summary>
        /// <param name="EquTypeFeatures"></param>
        /// <param name="FeaturesContent"></param>
        /// <param name="MaintenanceCycle"></param>
        /// <returns></returns>
        public static string saveEquTypeFeature(string EquTypeFeatures, string FeaturesContent, string MaintenanceCycle)
        {
            using (var conn = new SqlConnection(SqlConnString))
            {
                SqlCommand cmd = new SqlCommand();
                SqlTransaction transaction = null;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    cmd.Transaction = transaction;
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlParameter[] sqlPara = new SqlParameter[3];

                    sqlPara[0] = new SqlParameter("@EquTypeFeatures", EquTypeFeatures);
                    sqlPara[0].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[1] = new SqlParameter("@FeaturesContent", FeaturesContent);
                    sqlPara[1].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[2] = new SqlParameter("@MaintenanceCycle", MaintenanceCycle);
                    sqlPara[2].Direction = System.Data.ParameterDirection.Input;
                   
                    foreach (SqlParameter para in sqlPara)
                    {
                        cmd.Parameters.Add(para);
                    }
                    cmd.CommandText = "[usp_Equ_saveEquTypeFeature]";
                    cmd.ExecuteNonQuery();
                    transaction.Commit();
                   
                    return "success";
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return "falut";
                }
            }

        }

        /// <summary>
        /// 得到设备类型特性列表
        /// </summary>
        /// <param name="sortname"></param>
        /// <param name="sortorder"></param>
        /// <param name="EquTypeName"></param>
        /// <returns></returns>
        public static DataTable getEquTypeFeatureList(string sortname, string sortorder, string EquTypeName)
        {
            string str = "";
            DataTable dt = new DataTable();
            using (var conn = new SqlConnection(SqlConnString))
            {
                SqlCommand cmd = new SqlCommand();
                conn.Open();
                cmd.Connection = conn;

                str = "select * from EquTypeFeatures where EquTypeName = '" + EquTypeName + "'";
               
                str += "order by " + sortname + " " + sortorder + "";
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = str;
                SqlDataAdapter Datapter = new SqlDataAdapter(cmd);
                Datapter.Fill(dt);
                return dt;

            }

        }

        /// <summary>
        /// 编辑设备类型特性初始化界面
        /// </summary>
        /// <param name="FeatureID"></param>
        /// <returns></returns>
        public static List<EquTypeFeaturesInfo> initEquTypeFeaterEditForm(string FeatureID)
        {
            using (var conn = new SqlConnection(SqlConnString))
            {

                string str = "select * from EquTypeFeatures  where  ID='" + FeatureID + "' ";
                return conn.Query<EquTypeFeaturesInfo>(str).ToList();
            }

        }

        /// <summary>
        /// 保存编辑设备类型特性
        /// </summary>
        /// <param name="FeatureID"></param>
        /// <param name="EquTypeFeatures"></param>
        /// <param name="FeaturesContent"></param>
        /// <param name="MaintenanceCycle"></param>
        /// <returns></returns>
        public static string editEquTypeFeature(string FeatureID, string EquTypeFeatures, string FeaturesContent, string MaintenanceCycle)
        {
            using (var conn = new SqlConnection(SqlConnString))
            {
                SqlCommand cmd = new SqlCommand();
                SqlTransaction transaction = null;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    cmd.Transaction = transaction;
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlParameter[] sqlPara = new SqlParameter[4];

                    sqlPara[0] = new SqlParameter("@FeatureID", FeatureID);
                    sqlPara[0].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[1] = new SqlParameter("@EquTypeFeatures", EquTypeFeatures);
                    sqlPara[1].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[2] = new SqlParameter("@FeaturesContent", FeaturesContent);
                    sqlPara[2].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[3] = new SqlParameter("@MaintenanceCycle", MaintenanceCycle);
                    sqlPara[3].Direction = System.Data.ParameterDirection.Input;
                    
                    foreach (SqlParameter para in sqlPara)
                    {
                        cmd.Parameters.Add(para);
                    }
                    cmd.CommandText = "[usp_Equ_editEquTypeFeature]";
                    cmd.ExecuteNonQuery();
                    transaction.Commit();
                    
                    return "success";
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return "falut";
                }
            }

        }

        /// <summary>
        /// 新增设备类型界面  如果点击关闭按钮  就删除未分配到设备类型的特性
        /// </summary>
        /// <returns></returns>
        public static int deleteuntypefeature()
        {
            int affecgtRow = 0;

            using (var conn = new SqlConnection(SqlConnString))
            {
                string sql = "DELETE FROM  EquTypeFeatures WHERE EquTypeName = '';";
                affecgtRow = conn.Execute(sql);
            }
            return affecgtRow;
        }

        /// <summary>
        /// 保存新增设备类型   带有设备类型特性
        /// </summary>
        /// <param name="EquTypeName"></param>
        /// <param name="EquTypeRemark"></param>
        /// <param name="EquIndex"></param>
        /// <param name="EquTypeTexingID"></param>
        /// <returns></returns>
        public static string saveEquTypeWithTexing(string EquTypeName, string EquTypeRemark, string EquIndex, List<string> EquTypeTexingID)
        {
            using (var conn = new SqlConnection(SqlConnString))
            {
                SqlCommand cmd = new SqlCommand();
                SqlTransaction transaction = null;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    cmd.Transaction = transaction;
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlParameter[] sqlPara = new SqlParameter[6];
                    for (int i = 0; i < EquTypeTexingID.Count; i++)
                    {
                        if (i == 0)
                        {
                            sqlPara[0] = new SqlParameter("@EquTypeName", EquTypeName);
                            sqlPara[0].Direction = System.Data.ParameterDirection.Input;
                            sqlPara[1] = new SqlParameter("@EquTypeRemark", EquTypeRemark);
                            sqlPara[1].Direction = System.Data.ParameterDirection.Input;
                            sqlPara[2] = new SqlParameter("@EquIndex", EquIndex);
                            sqlPara[2].Direction = System.Data.ParameterDirection.Input;
                            sqlPara[3] = new SqlParameter("@EquTypeTexingID ", EquTypeTexingID[i]);
                            sqlPara[3].Direction = System.Data.ParameterDirection.Input;
                            sqlPara[4] = new SqlParameter("@Count ", i);
                            sqlPara[4].Direction = System.Data.ParameterDirection.Input;
                            sqlPara[5] = new SqlParameter("@CatchFlag", 0);
                            sqlPara[5].Size = 10;
                            sqlPara[5].Direction = System.Data.ParameterDirection.Output;

                            foreach (SqlParameter para in sqlPara)
                            {
                                cmd.Parameters.Add(para);
                            }
                        }
                        else
                        {
                            cmd.Parameters[3].Value = EquTypeTexingID[i];
                            cmd.Parameters[4].Value = i;
                        }
                        cmd.CommandText = "[usp_Equ_saveEquTypeWithTexing]";
                        cmd.ExecuteNonQuery();
                    }
                    transaction.Commit();
                    if (int.Parse(sqlPara[5].Value.ToString()) == 1)
                    {
                        return "hasexit";
                    }
                    return "success";
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return "falut";
                }
            }

        }

        /// <summary>
        /// 保存  编辑设备信息
        /// </summary>
        /// <param name="EquNo"></param>
        /// <param name="Remark"></param>
        /// <returns></returns>
        public static string saveeditequ(string EquNo, string EquNewNo, string Remark)
        {
            using (var conn = new SqlConnection(SqlConnString))
            {
                SqlCommand cmd = new SqlCommand();
                SqlTransaction transaction = null;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    cmd.Transaction = transaction;
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlParameter[] sqlPara = new SqlParameter[3];

                    sqlPara[0] = new SqlParameter("@EquNo", EquNo);
                    sqlPara[0].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[1] = new SqlParameter("@EquNewNo", EquNewNo);
                    sqlPara[1].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[2] = new SqlParameter("@Remark", Remark);
                    sqlPara[2].Direction = System.Data.ParameterDirection.Input;
                    

                    foreach (SqlParameter para in sqlPara)
                    {
                        cmd.Parameters.Add(para);
                    }
                    cmd.CommandText = "[usp_Equ_saveeditequ]";
                    cmd.ExecuteNonQuery();
                    transaction.Commit();

                    return "success";
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return "falut";
                }
            }

        }

        /// <summary>
        /// 保存  编辑设备故障信息
        /// </summary>
        /// <param name="AlarmID"></param>
        /// <param name="AlarmName"></param>
        /// <param name="AlarmHandle"></param>
        /// <param name="note"></param>
        /// <returns></returns>
        public static string saveeditequalarm(string AlarmID, string AlarmName, string AlarmHandle, string note)
        {
            using (var conn = new SqlConnection(SqlConnString))
            {
                SqlCommand cmd = new SqlCommand();
                SqlTransaction transaction = null;
                try
                {
                    conn.Open();
                    transaction = conn.BeginTransaction();
                    cmd.Transaction = transaction;
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlParameter[] sqlPara = new SqlParameter[4];

                    sqlPara[0] = new SqlParameter("@AlarmID", AlarmID);
                    sqlPara[0].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[1] = new SqlParameter("@AlarmName", AlarmName);
                    sqlPara[1].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[2] = new SqlParameter("@AlarmHandle", AlarmHandle);
                    sqlPara[2].Direction = System.Data.ParameterDirection.Input;
                    sqlPara[3] = new SqlParameter("@note", note);
                    sqlPara[3].Direction = System.Data.ParameterDirection.Input;

                    foreach (SqlParameter para in sqlPara)
                    {
                        cmd.Parameters.Add(para);
                    }
                    cmd.CommandText = "[usp_Equ_saveeditequalarm]";
                    cmd.ExecuteNonQuery();
                    transaction.Commit();

                    return "success";
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return "falut";
                }
            }

        }


        public static List<CustomerEquFangShaKind> initfangshabyid(string FangshaID)
        {
            using (SqlConnection connection = new SqlConnection(SqlConnString))
            {
                string sql = "select * from CustomerEquFangShaKind  where  ID='" + FangshaID + "' ";
                return connection.Query<CustomerEquFangShaKind>(sql, null, null, true, null, null).ToList<CustomerEquFangShaKind>();
            }
        }

        public static string saveShengChanfangshakind(string FangShaKind, string CustomerID, string OutPutTarget, string FangShaDate)
        {
            string str;
            using (SqlConnection connection = new SqlConnection(SqlConnString))
            {
                SqlCommand command = new SqlCommand();
                SqlTransaction transaction = null;
                try
                {
                    connection.Open();
                    transaction = connection.BeginTransaction();
                    command.Transaction = transaction;
                    command.Connection = connection;
                    command.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] parameterArray = new SqlParameter[4];
                    parameterArray[0] = new SqlParameter("@FangShaKind", FangShaKind);
                    parameterArray[0].Direction = ParameterDirection.Input;
                    parameterArray[1] = new SqlParameter("@OutPutTarget", OutPutTarget);
                    parameterArray[1].Direction = ParameterDirection.Input;
                    parameterArray[2] = new SqlParameter("@FangShaDate", FangShaDate);
                    parameterArray[2].Direction = ParameterDirection.Input;
                    parameterArray[3] = new SqlParameter("@CustomerID", CustomerID);
                    parameterArray[3].Direction = ParameterDirection.Input;
                    foreach (SqlParameter parameter in parameterArray)
                    {
                        command.Parameters.Add(parameter);
                    }
                    command.CommandText = "[usp_Equ_saveShengChanfangshakind]";
                    command.ExecuteNonQuery();
                    transaction.Commit();
                    str = "success";
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    str = "falut";
                }
            }
            return str;
        }

        public static string editShengChanfangshakind(string FangShaKind, string OutPutTarget, string FangShaDate, string ID)
        {
            string str;
            using (SqlConnection connection = new SqlConnection(SqlConnString))
            {
                SqlCommand command = new SqlCommand();
                SqlTransaction transaction = null;
                try
                {
                    connection.Open();
                    transaction = connection.BeginTransaction();
                    command.Transaction = transaction;
                    command.Connection = connection;
                    command.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] parameterArray = new SqlParameter[4];
                    parameterArray[0] = new SqlParameter("@FangShaKind", FangShaKind);
                    parameterArray[0].Direction = ParameterDirection.Input;
                    parameterArray[1] = new SqlParameter("@OutPutTarget", OutPutTarget);
                    parameterArray[1].Direction = ParameterDirection.Input;
                    parameterArray[2] = new SqlParameter("@FangShaDate", FangShaDate);
                    parameterArray[2].Direction = ParameterDirection.Input;
                    parameterArray[3] = new SqlParameter("@ID", ID);
                    parameterArray[3].Direction = ParameterDirection.Input;
                    foreach (SqlParameter parameter in parameterArray)
                    {
                        command.Parameters.Add(parameter);
                    }
                    command.CommandText = "[usp_Equ_editShengChanfangshakind]";
                    command.ExecuteNonQuery();
                    transaction.Commit();
                    str = "success";
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    str = "falut";
                }
            }
            return str;
        }

        public static List<CustomerEquFangShaKind> initShengChanSetInfo(string CustomerID)
        {
            using (SqlConnection connection = new SqlConnection(SqlConnString))
            {
                string sql = "select a.*,b.CustomName CustomeName from CustomerEquFangShaKind a  left join CustomInfo b on  a.CustomerID=b.ID where  a.CustomerID='" + CustomerID + "' ";
                return connection.Query<CustomerEquFangShaKind>(sql, null, null, true, null, null).ToList<CustomerEquFangShaKind>();
            }
        }

 

    }
}
